package com.ming.closeto.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.ming.closeto.R;
import com.ming.closeto.adapter.LifeStyleAdapter;
import com.ming.closeto.bean.LifeStyleBean;
import com.ming.closeto.entity.Constant;
import com.ming.closeto.impl.WeatherApi;
import com.ming.closeto.model.LifeStyleModel;
import com.ming.closeto.model.NowWeatherModel;
import com.ming.closeto.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MeFragment extends Fragment {

    private Retrofit retrofit;
    private WeatherApi api;
    private TextView tv_city;
    private TextView tv_cond_txt_tmp;
    private TextView tv_wind_dir_sc;

    private RecyclerView mLifeStyleView;
    private LifeStyleAdapter mLifeStyleAdapter;
    private List<LifeStyleBean> mList = new ArrayList<>();
    private ScrollView mScrollView;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, null);
        initView(view);
        return view;
    }

    private void initView(View view) {

        tv_city = (TextView) view.findViewById(R.id.tv_city);
        tv_cond_txt_tmp = (TextView) view.findViewById(R.id.tv_cond_txt_tmp);
        tv_wind_dir_sc = (TextView) view.findViewById(R.id.tv_wind_dir_sc);
        mLifeStyleView = view.findViewById(R.id.mLifeStyleView);
        mLifeStyleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mLifeStyleAdapter = new LifeStyleAdapter(getActivity(), mList);
        mLifeStyleView.setAdapter(mLifeStyleAdapter);

        mScrollView = view.findViewById(R.id.mScrollView);

        initWeatherApi();
    }

    private void initWeatherApi() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.WEATHER_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        api = retrofit.create(WeatherApi.class);
        // 获取实时天气
        api.getNowWeather(Constant.WEATHER_CITY, Constant.WEATHER_KEY)
                .enqueue(new Callback<NowWeatherModel>() {
                    @Override
                    public void onResponse(Call<NowWeatherModel> call, Response<NowWeatherModel> response) {
                        if (response.isSuccessful()) {
                            NowWeatherModel model = response.body();
                            parsingNowWeather(model);
                        }
                    }
                    @Override
                    public void onFailure(Call<NowWeatherModel> call, Throwable t) {

                    }
                });
        // 获取生活指数
        api.getLifeStyle(Constant.WEATHER_CITY, Constant.WEATHER_KEY)
                .enqueue(new Callback<LifeStyleModel>() {
                    @Override
                    public void onResponse(Call<LifeStyleModel> call, Response<LifeStyleModel> response) {
                        if (response.isSuccessful()) {
                            LifeStyleModel model = response.body();
                            parsingLifeStyle(model);
                        }
                    }

                    @Override
                    public void onFailure(Call<LifeStyleModel> call, Throwable t) {

                    }
                });
    }

    /**
     * 解析今天的天气
     *
     * @param model
     */
    private void parsingNowWeather(NowWeatherModel model) {

        NowWeatherModel.HeWeather6Bean.BasicBean basicBean = model.getHeWeather6().get(0).getBasic();
        NowWeatherModel.HeWeather6Bean.NowBean nowBean = model.getHeWeather6().get(0).getNow();
        //Toast.makeText(getActivity(), model.getHeWeather6().get(0).getBasic() + "", Toast.LENGTH_LONG).show();
        tv_city.setText(basicBean.getLocation());
        tv_cond_txt_tmp.setText(nowBean.getCond_txt() + " | " + nowBean.getTmp() + "℃");
        tv_wind_dir_sc.setText(nowBean.getWind_dir() + " | " + nowBean.getWind_sc());
    }

    /**
     * 解析生活指数
     * @param model
     */
    private void parsingLifeStyle(LifeStyleModel model) {
        //Toast.makeText(getActivity(), "" + model.getHeWeather6().get(0).getLifestyle().get(0).getTxt(), Toast.LENGTH_LONG).show();
        List<LifeStyleModel.HeWeather6Bean.LifestyleBean> list = model.getHeWeather6().get(0).getLifestyle();
        for (int i=0; i<list.size(); i++) {
            LifeStyleModel.HeWeather6Bean.LifestyleBean lifestyleBean= list.get(i);
            LifeStyleBean bean = new LifeStyleBean();
            bean.setBrf(lifestyleBean.getBrf());
            bean.setTxt(lifestyleBean.getTxt());
            bean.setType(lifestyleBean.getType());
            mList.add(bean);
        }
        mLifeStyleAdapter.notifyDataSetChanged();

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (mScrollView != null) {
                mScrollView.post(new Runnable() {
                    @Override
                    public void run() {
                        mScrollView.fullScroll(ScrollView.FOCUS_UP);
                    }
                });
            }
        }
    }
}

